﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using ProjetoFinal.Resources;
using ProjetoFinal.Resources.ReedSolomon;

namespace ProjetoFinal
{

    public partial class Form1 : Form
    {
        private int radioSelected = -1;
        private int lastEncodedCBSize = 0;


        public Form1()
        {
            InitializeComponent();
        }

        public String encodeData(String dataString, int numErrorCorrectionBytes)
        {

            lastEncodedCBSize = numErrorCorrectionBytes;

            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            Byte[] data = encoding.GetBytes(dataString);



            int totalBytes = numErrorCorrectionBytes + data.Length;
            int[] dataInts = new int[totalBytes];

            char[] resultado = new char[totalBytes];


            if (data == null || data.Length == 0)
            {
                return null;
            }

            if ((data.Length + numErrorCorrectionBytes) > 256)
            {
                return "Máximo de bytes = 256";
            }

            for (int i = 0; i < data.Length; i++)
            {
                dataInts[i] = data[i] & 0xFF;
            }

            RSUtils.encode(dataInts, numErrorCorrectionBytes, radioSelected);

            for (int i = 0; i < dataInts.Length; i++)
            {
                resultado[i] = char.ConvertFromUtf32(dataInts[i])[0];
            }

            return new String(resultado);
        }

        public String decodeData(String dataString)
        {
            int numErrorCorrectionBytes = lastEncodedCBSize;

            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            Byte[] data = encoding.GetBytes(dataString);

            int totalBytes = data.Length;
            int[] dataInts = new int[totalBytes];

            char[] resultado = new char[totalBytes];

            if (data == null || data.Length == 0)
            {
                return null;
            }

            if ((data.Length + numErrorCorrectionBytes) > 256)
            {
                return "Máximo de bytes = 256";
            }


            for (int i = 0; i < data.Length; i++)
            {
                dataInts[i] = data[i] & 0xFF;
            }

            Console.WriteLine(RSUtils.decode(dataInts, numErrorCorrectionBytes, radioSelected));

            for (int i = 0; i < dataInts.Length - numErrorCorrectionBytes; i++)
            {
                resultado[i] = char.ConvertFromUtf32(dataInts[i])[0];
            }

            return new String(resultado);
        }

        private void Form1_Load(object sender, EventArgs e)
        {


        }

        private void buttonCodificar_Click(object sender, EventArgs e)
        {
            String textoParaCodificar = textBoxParaCodificar.Text;

            int tamanhoCodificacao = 10;

            try
            {
                tamanhoCodificacao = Convert.ToInt32(textBoxCodeSize.Text);
            }
            catch { }
            textBoxParaDecodificar.Text = encodeData(textoParaCodificar, tamanhoCodificacao);

        }

        private bool[] encodeBits(bool[] bits, int numErrorCorrectionBytes)
        {

            // BitConverter.GetBytes(bits);

            return null;
        }

        private void buttonDecodificar_Click(object sender, EventArgs e)
        {
            String textoParaDecodificar = textBoxParaDecodificar.Text;

            textBoxParaCodificar.Text = decodeData(textoParaDecodificar);
        }

        private void textBoxParaCodificar_TextChanged(object sender, EventArgs e)
        {
            textBoxCorrespondentHex.Text = CVUtils.binaryToHex(textBoxParaCodificar.Text);
        }

        private void buttonCodificarHex_Click(object sender, EventArgs e)
        {

        }

        private void buttonCode4Bit_Click(object sender, EventArgs e)
        {
            int tamanhoCodigo = 10;
            string[] numeros = textBoxParaCodificar.Text.Split(';');

            try
            {
                tamanhoCodigo = Convert.ToInt32(textBoxCodeSize.Text);
            }
            catch { }

            lastEncodedCBSize = tamanhoCodigo;


            int[] numerosData = new int[numeros.Length + tamanhoCodigo];

            for (int i = 0; i < numeros.Length; i++)
            {
                try
                {
                    numerosData[i] = Convert.ToInt32(numeros[i]);
                }
                catch { }  
            }

            RSUtils.encode(numerosData, tamanhoCodigo, radioSelected);

            textBoxParaDecodificar.Text = "";

            for (int i = 0; i < numerosData.Length - 1; i++)
            {
                textBoxParaDecodificar.Text += numerosData[i] + ";";
            }

            textBoxParaDecodificar.Text += numerosData[numerosData.Length-1] + "";

        }

        private void buttonDecode4Bit_Click(object sender, EventArgs e)
        {
            int tamanhoCodigo = lastEncodedCBSize;
            string[] numeros = textBoxParaDecodificar.Text.Split(';');

            try
            {
                tamanhoCodigo = Convert.ToInt32(textBoxCodeSize.Text);
            }
            catch { }

            int[] numerosData = new int[numeros.Length];

            for (int i = 0; i < numeros.Length; i++)
            {
                try
                {
                    numerosData[i] = Convert.ToInt32(numeros[i]);
                }
                catch { }  
            }

            toolStripStatusLabel1.Text = "";

            toolStripStatusLabel1.Text += "Decodificado: " + RSUtils.decode(numerosData, tamanhoCodigo, radioSelected) + " ";

            textBoxResultado.Text = "";

            for (int i = 0; i < numerosData.Length - 1 - tamanhoCodigo; i++)
            {
                textBoxResultado.Text += numerosData[i] + ";";
            }

            textBoxResultado.Text += numerosData[numerosData.Length - 1 - tamanhoCodigo] + "";

        }

        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton16.Checked == true)
            {
                radioSelected = 16;
            }
            else if (radioButton256.Checked == true)
            {
                radioSelected = 256;
            }
            else if (radioButton1024.Checked == true)
            {
                radioSelected = 1024;
            }
            else if (radioButton4096.Checked == true)
            {
                radioSelected = 4096;
            }
        }

    }
}
